//
//  GuestCouponDetailViewController.m
//  penzer
//
//  Created by 还带大道 on 2018/11/8.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "GuestCouponDetailViewController.h"
#import <WebKit/WebKit.h>
#import "CouponDetailFV.h"
#import "CouponDetailHV.h"
#import "GuestCouponCommentListViewController.h"
#import "GuestCouponCommentTableViewCell.h"

#define kEndH 40

@interface GuestCouponDetailViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) WKWebView *webView;
@property (nonatomic,strong) UILabel *headerLabel;

@end

@implementation GuestCouponDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNavigation:YES andTitle:@"卡券详情" andNaviColor:[UIColor whiteColor] andTitleColor:nil];
    [self.view addSubview:self.tableView];
    [self setupTableView];
}


- (void)dealloc
{
    [_webView.scrollView removeObserver:self forKeyPath:@"contentOffset"];
}


- (void)setupTableView
{
    CouponDetailHV *headerV = [CouponDetailHV shareView];
    self.tableView.tableHeaderView = headerV;
    headerV.commentBlock = ^{
        [self goCommentList];
    };
    headerV.height = 220;
    
    CouponDetailFV *footerV = [CouponDetailFV shareView];
    __weak typeof(footerV) weakO = footerV;
    footerV.backgroundColor = [UIColor whiteColor];
    footerV.refreshBlock = ^(CGFloat height) {
        weakO.height = height;
        self.tableView.tableFooterView = weakO;
    };
    self.tableView.tableFooterView = footerV;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GuestCouponCommentTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GuestCouponCommentTableViewCell class])];
}


- (void)goCommentList
{
    GuestCouponCommentListViewController *list = [GuestCouponCommentListViewController new];
    [self pushSecondVC:list];
}



#pragma mark--UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GuestCouponCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GuestCouponCommentTableViewCell class])];
    return cell;
}



#pragma makr - UIScrollViewDelegate
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    CGFloat offsetY = scrollView.contentOffset.y;
    RZLog(@"---offsetY= %f",offsetY);
    if([scrollView isKindOfClass:[UITableView class]]){ //tableView界面上的滚动

        //阈值判断
        CGFloat valueNum = self.tableView.contentSize.height - self.tableView.height + kEndH;
        if (offsetY > 0 && offsetY > valueNum){
            // 进入图文详情的动画
            if(![self.view.subviews containsObject:self.webView]){
                [self.view addSubview:self.webView];
                [self.webView addSubview:self.headerLabel];
            }
            [self goToDetailAnimation];
        }
        
    }else{// webView页面上的滚动
        if(offsetY < -kEndH){
            // 返回基本详情界面的动画
            [self backToFirstPageAnimation];
        }
    }
}




// KVO观察
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if(object == _webView.scrollView && [keyPath isEqualToString:@"contentOffset"]){
        [self headLabAnimation:[change[@"new"] CGPointValue].y];
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}




// 头部提示文本动画
- (void)headLabAnimation:(CGFloat)offsetY
{
    self.headerLabel.alpha = -offsetY/kEndH;
    if(offsetY < -kEndH){
        self.headerLabel.text = @"释放回到卡券详情";
    }else{
        self.headerLabel.text = @"下拉回到卡券详情";
    }
}




// 进入详情的动画
- (void)goToDetailAnimation
{
    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        CGRect rect_w = self.webView.frame;
        rect_w.origin.y = kTopHeight;
        self.webView.frame = rect_w;
        
        CGRect rect_t = self.tableView.frame;
        rect_t.origin.y = -rect_t.size.height;
        self.tableView.frame = rect_t;
        
    } completion:^(BOOL finished) {
        
    }];
}



// 返回第一个界面的动画
- (void)backToFirstPageAnimation
{
    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        CGRect rect_t = self.tableView.frame;
        rect_t.origin.y = kTopHeight;
        self.tableView.frame = rect_t;
        
        CGRect rect_w = self.webView.frame;
        rect_w.origin.y = CGRectGetMaxY(rect_t);
        self.webView.frame = rect_w;
        
    } completion:^(BOOL finished) {
        
    }];
}


#pragma mark--getter&&setter

- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.frame = CGRectMake(0, kTopHeight, RZ_SCREEN_WIDTH, RZ_SCREEN_HEIGHT-kTopHeight);
    }
    return _tableView;
}


- (WKWebView *)webView
{
    if(!_webView){
        _webView = [[WKWebView alloc] init];
        _webView.scrollView.delegate = self;
        _webView.frame = CGRectMake(0, CGRectGetMaxY(self.tableView.frame), RZ_SCREEN_WIDTH, RZ_SCREEN_HEIGHT-kTopHeight);
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://vasterroad.com/"]]];
        [ _webView.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    }
    return _webView;
}


- (UILabel *)headerLabel
{
    if(!_headerLabel){
        _headerLabel = [UILabel new];
        _headerLabel.backgroundColor = [UIColor clearColor];
        _headerLabel.frame = CGRectMake(0, 0, RZ_SCREEN_WIDTH, 45);
        _headerLabel.textAlignment = NSTextAlignmentCenter;
        _headerLabel.textColor = RZ_COLOR(155, 155, 155);
        _headerLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
    }
    return _headerLabel;
}

@end

